﻿
/*=========================================
* Author: Administrator
* DateTime:2017/6/22 16:47:56
* Description:$safeprojectname$
==========================================*/

using System.Collections;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace OrderSystem
{
    public class ClientItemView : MonoBehaviour
    {
        private Text text = null;
        private Image image = null;
        public ClientItem clientData = null;
        private UnityAction<ClientItem> PayWaiter;
        private UnityAction<ClientItem> ClientLeave;
        private void Awake()
        {
            text = transform.Find("Id").GetComponent<Text>();
            image = transform.GetComponent<Image>();
        }

        public void InitClient( ClientItem data,UnityAction<ClientItem> payAction, UnityAction<ClientItem> LeaveClient)
        {

            this.clientData = data;
            PayWaiter = payAction;
            ClientLeave = LeaveClient;
            UpdateState(); 
        }

        private void UpdateState( )
        {
            Color color = Color.white;
            if ( this.clientData.state.Equals(E_ClientState.Idle) )
                color = Color.green;
            else if ( this.clientData.state.Equals(E_ClientState.WaitMenu) )
                color = Color.yellow;
            else if ( this.clientData.state.Equals(E_ClientState.EatFood) )
            {
                color = Color.red;
                StartCoroutine(eatting());
            }
            else if(this.clientData.state.Equals(E_ClientState.FinishPay))
            {
                color = Color.cyan;
                StartCoroutine(WaitLeave());
            }
            image.color = color;
            text.text = clientData.ToString();
        }

        private IEnumerator eatting( float time = 5 )
        {
            Debug.Log(clientData.id + "号桌客人正在就餐");
            yield return new WaitForSeconds(time);
            image.color = Color.white;
            PayWaiter(clientData);
        }
        private IEnumerator WaitLeave(float time = 3)
        {
            Debug.Log(clientData.id + "准备离店");
            yield return new WaitForSeconds(time);
            ClientLeave(clientData);
        }
    }
}